package com.tiecode.plugin.regex.action;

import android.graphics.drawable.Drawable;

import com.tiecode.develop.component.api.multiple.CreateWindowAction;
import com.tiecode.develop.component.api.multiple.Window;
import com.tiecode.develop.component.widget.dialog.BottomDialog;
import com.tiecode.develop.component.widget.editor.TieCodeEditor;
import com.tiecode.develop.util.firstparty.image.DrawableUtils;
import com.tiecode.develop.util.firstparty.net.URIUtils;
import com.tiecode.plugin.PluginEnvironment;
import com.tiecode.plugin.action.page.code.CodeBodyPageAction;
import com.tiecode.plugin.regex.R;
import com.tiecode.plugin.regex.editor.LanguageRegex;
import com.tiecode.plugin.regex.editor.RegexEntity;
import com.tiecode.plugin.regex.editor.decision.RegexDecision;
import com.tiecode.plugin.regex.util.RegexTestUtils;

import java.net.URI;
import java.util.Collection;

import io.github.rosemoe.sora.event.EventReceiver;
import io.github.rosemoe.sora.event.SideIconClickEvent;
import io.github.rosemoe.sora.event.Unsubscribe;

public class RegexCodeBodyAction extends CodeBodyPageAction {
    public final static String SCHEMA_REGEX_EDIT = "regex-edit";
    public final static String SCHEMA_REGEX_TEST = "regex-test";
    public static Drawable testDrawable;

    @Override
    public void onCreate() {
        testDrawable = DrawableUtils.getDrawable(
                PluginEnvironment.getContext(this),
                R.drawable.ic_test
        );
        registerSchemeAction(SCHEMA_REGEX_EDIT, new CreateWindowAction() {
            @Override
            public Window create(URI uri) {
                TieCodeEditor editor = (TieCodeEditor) createEditor();
                return editor;
            }
        });
    }

    @Override
    public void onWindowCreate(Window window, URI uri) {
        if (!(window instanceof TieCodeEditor)) {
            return;
        }
        TieCodeEditor editor = (TieCodeEditor) window;
        String suffix = URIUtils.getURISuffix(uri);
        if ("rmt".equals(suffix)) {
            Collection<RegexEntity> entities = RegexDecision.getRequestEntities();
            StringBuilder result = new StringBuilder();
            for (RegexEntity entity : entities) {
                String tmp = RegexTestUtils.doTest(entity);
                result.append("用例：").append(entity.name).append("\n");
                result.append("表达式：").append(entity.regex).append("\n");
                result.append("内容：").append(entity.content).append("\n");
                result.append("结果：").append(tmp).append("\n\n");
            }
            editor.setText(result.toString());
        }
        if (!"rmc".equals(suffix)) {
            return;
        }
        editor.setText("# 1\n表达式\n测试内容");
        editor.setEditorLanguage(new LanguageRegex());
        editor.subscribeEvent(SideIconClickEvent.class, new EventReceiver<SideIconClickEvent>() {
            @Override
            public void onReceive(SideIconClickEvent event, Unsubscribe unsubscribe) {
                int line = event.getClickedIcon().getLine();
                RegexEntity entity = RegexDecision.getRequestEntity(line);
                if (entity != null) {
                    doTest(entity);
                    editor.hideSoftInput();
                }
            }
        });
    }

    private void doTest(RegexEntity entity) {
        String result = RegexTestUtils.doTest(entity);
        BottomDialog dialog = new BottomDialog(getActivity()).builder();
        dialog.setTitle(entity.name);
        dialog.setContent(result);
        dialog.show();
    }
}
